package com.compass.doctor.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.R
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutBuyerItemBinding
import com.compass.doctor.databinding.LayoutLatestBuyerItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.saveAs
import com.compass.framework.glide.loadRes
import com.compass.framework.glide.loadResRound
import com.compass.framework.glide.setUrl
import com.compass.framework.glide.setUrlCircle
import com.compass.framework.model.OrderSpecial
import com.compass.framework.utils.BigDecimalUtils
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.ViewUtils
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getColorFromResource

/**
 * Description： 最新购买的人
 * Date:2023/12/24 13:47
 */


class LatestBuyerAdapter constructor(private var itemType: Int) :

    BaseMultiItemAdapter<OrderSpecial>() {

    private lateinit var onChatBottomClick: OnCustomButtonClick

    //1为首页布局 2为列表布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }

    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onChatBottomClick = onChatBottomClick
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: OrderSpecial?,
        position: Int
    ) {
        if (item == null) return
        //  //1、知识中心最新购买， 2(其他)、最新购买列表
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutLatestBuyerItemBinding>()
                binding.apply {
                    if (position == getData().size - 1) lineBottom.visibility =
                        View.GONE else lineBottom.visibility = View.VISIBLE
                    ivHead.setUrlCircle(item.patientAvatar)
                    tvName.text = item.getUserNam()
                    tvTime.text = DateUtils.getTimeDescriptionByDate(item.createTime)
                    tvPrice.text = "￥${BigDecimalUtils.divide(item.price)}"

                    if (item.type==1){
                        //专栏
                        ivCommunication.loadRes(R.mipmap.icon_special_olumn)
                    }else{
                        //资讯
                        ivCommunication.setUrl(item.coverImg)
                    }
                    ViewUtils.setClipViewCornerRadius(ivCommunication, dpToPx(5))

                }
            }

            else -> {
                val binding = holder.binding.saveAs<LayoutBuyerItemBinding>()
                binding.apply {
                    tvTitle.text = item.title
                    tvPrice.text = "￥${BigDecimalUtils.divide(item.price)}"
                    ivHead.setUrlCircle(item.patientAvatar)
                    tvName.text = item.getUserNam()
                    tvTime.text = DateUtils.getTimeDescriptionByDate(item.createTime)
                    if (item.type==1){
                        //专栏
                        tvCommunication.visibility=View.INVISIBLE
                        tvDiagnose.visibility=View.INVISIBLE
                        tvLike.visibility=View.INVISIBLE
                        tvType.text="【专栏订阅】"
                        tvType.setTextColor(getColorFromResource(R.color.color_de9037))

                    }else{
                        tvType.text="【文章购买】"
                        tvType.setTextColor(getColorFromResource(R.color.color_5795ff))
                        tvCommunication.visibility=View.VISIBLE
                        tvDiagnose.visibility=View.VISIBLE
                        tvLike.visibility=View.VISIBLE
                        //资讯
                        tvDiagnose.text = "浏览 ${item.readNum}"
                        tvLike.text = "点赞 ${item.likeNum}"
                        tvCommunication.setUrl(item.coverImg)
                        ViewUtils.setClipViewCornerRadius(tvCommunication, dpToPx(5))
                    }
                }
            }
        }
    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutLatestBuyerItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutBuyerItemBinding.inflate(layoutInflater, parent, false)
            }
        }
    }
}